<!doctype html>
<html>
  <head>
    <title>Notifications: Reply reflection in the "notificationclick" event.</title>
    <script src="../resources/testharness.js"></script>
    <script src="../resources/testharnessreport.js"></script>
    <script src="../serviceworker/resources/test-helpers.js"></script>
    <script src="resources/test-helpers.js"></script>
  </head>
  <body>
    <script>
      // Tests that the reply property of the "notificationclick" event in the
      // Service Worker accurately reflects the reply entered.

      async_test(function(test) {
          var scope = 'resources/scope/' + location.pathname;
          var script = 'instrumentation-service-worker.js';
          var port;

          var options = {
            actions: [{ action: 'actionName', title: 'ActionTitle', type: 'text' }]
          };

          testRunner.setPermission('notifications', 'granted', location.origin, location.origin);
          getActiveServiceWorkerWithMessagePort(test, script, scope).then(function(info) {
              port = info.port;
              // (1) Tell the Service Worker to display a Web Notification.
              return sendCommand(port, {
                  command: 'show',

                  title: scope,
                  options: options
              });
          }).then(function(data) {
              // (2) Confirm that the service worker displayed the notification successfully.
              assert_true(data.success, 'The notification must have been displayed.');

              var expectedReplies = [];
              // (3) Simulate some clicks on the notification, with and without replies.
              // (3.1) Simulate a reply to the notification text action.
              testRunner.simulateWebNotificationClick(scope, 0 /* action_index */, 'My reply.');
              expectedReplies.push('My reply.');

              // (3.2) Simulate an empty reply to the notification text action.
              testRunner.simulateWebNotificationClick(scope, 0 /* action_index */, '');
              expectedReplies.push('');

              // (3.3) Simulate a click on the notification body (reply should be null).
              testRunner.simulateWebNotificationClick(scope);
              expectedReplies.push(null);

              port.addEventListener('message', function(event) {
                  // (4) Listen for confirmation from the Service Worker that the
                  // notification has been clicked on. Make sure that the reply
                  // property set on the NotificationEvent object is as expected.
                  assert_equals(event.data.command, 'click', 'The notification was expected to be clicked.');

                  assert_equals(event.data.reply, expectedReplies.shift());

                  if (expectedReplies.length === 0)
                      test.done();
              });
          }).catch(unreached_rejection(test));

      }, 'NotificationEvent reply property should reflect the reply that was entered');
    </script>
  </body>
</html>
